package com.chexiehui.jc.mvp.login;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.StringUtils;
import com.chexiehui.jc.Constants;
import com.chexiehui.jc.base.ApiCallback;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BasePresenter;
import com.chexiehui.jc.entity.AuthInfo;
import com.chexiehui.jc.entity.UserInfo;

/**
 * @Description 登录
 * @Auth SongJun
 * @Date 2019/11/20 10:05
 **/
public class LoginPresenter extends BasePresenter<LoginCovenant.View, LoginCovenant.Stores>
        implements LoginCovenant.Presenter {

    public LoginPresenter(LoginCovenant.View view) {
        attachView(view);
    }


    @Override
    public void getCodeByPhoneLogin(String phone) {
        if (StringUtils.isTrimEmpty(phone)) {
            mvpView.showToast("请输入手机号码");
            return;
        }
        if (!RegexUtils.isMobileSimple(phone)) {
            mvpView.showToast("请输入正确的手机号码");
            return;
        }
        mvpView.showLoading("发送验证码");
        addSubscription(appStores.getCode(phone),
                new ApiCallback<BaseModel<Object>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<Object> model) {
                        if (model.getResultCode() == Constants.REQUEST_OK_CODE) {
                            mvpView.hide();
                            mvpView.onGetCodeByPhoneLoginSuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.hide();
                        mvpView.onGetCodeByPhoneLoginFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void phoneLogin(String phone, String code) {
        if (StringUtils.isTrimEmpty(phone)) {
            mvpView.showToast("请输入手机号码");
            return;
        }
        if (!RegexUtils.isMobileSimple(phone)) {
            mvpView.showToast("请输入正确的手机号码");
            return;
        }
        if (StringUtils.isTrimEmpty(code)) {
            mvpView.showToast("请输入验证码");
            return;
        }
        if (code.trim().length() != 6) {
            mvpView.showToast("请输入6位验证码");
            return;
        }
        mvpView.showLoading("正在登录");
        addSubscription(appStores.phoneLogin(phone, code, mvpView.getIm()),
                new ApiCallback<BaseModel<AuthInfo>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<AuthInfo> model) {
                        if (model.getResultCode() == Constants.REQUEST_OK_CODE
                                && model.getData() != null) {
                            mvpView.onPhoneLoginSuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.hide();
                        mvpView.onPhoneLoginFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void accountLogin(String phone, String password) {
        if (StringUtils.isTrimEmpty(phone)) {
            mvpView.showToast("请输入手机号码");
            return;
        }
        if (!RegexUtils.isMobileSimple(phone)) {
            mvpView.showToast("请输入正确的手机号码");
            return;
        }
        if (StringUtils.isTrimEmpty(password)) {
            mvpView.showToast("请输入密码");
            return;
        }
        if (password.length() < 6 || password.length() > 16) {
            mvpView.showToast("请输入6~16位字符数字组合密码");
            return;
        }
        mvpView.showLoading("正在登录");
        addSubscription(appStores.accountLogin(phone, password, mvpView.getIm()),
                new ApiCallback<BaseModel<AuthInfo>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<AuthInfo> model) {
                        if (model.getResultCode() == Constants.REQUEST_OK_CODE
                                && model.getData() != null) {
                            mvpView.onAccountLoginSuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.hide();
                        mvpView.onAccountLoginFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void getUserInfo() {
        addSubscription(appStores.getUserInfo(""),
                new ApiCallback<BaseModel<UserInfo>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<UserInfo> model) {
                        if (model.getResultCode() == Constants.REQUEST_OK_CODE
                                && model.getData() != null) {
                            mvpView.hide();
                            mvpView.onGetUserInfoSuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.hide();
                        mvpView.onGetUserInfoFailure(new BaseModel<>(code, msg));
                    }
                });
    }

}